package com.cxcs.cxcsojbackendserviceclient.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cxcs.cxcsojbackendcommon.common.ErrorCode;
import com.cxcs.cxcsojbackendcommon.constant.UserConstant;
import com.cxcs.cxcsojbackendcommon.exception.BusinessException;
import com.cxcs.cxcsojbackendmodel.dto.user.UserQueryRequest;
import com.cxcs.cxcsojbackendmodel.entity.User;
import com.cxcs.cxcsojbackendmodel.enums.UserRoleEnum;
import com.cxcs.cxcsojbackendmodel.vo.LoginUserVO;
import com.cxcs.cxcsojbackendmodel.vo.UserVO;
import org.springframework.beans.BeanUtils;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.util.Collection;
import java.util.List;

/**
 *  【远程接口】 客户端 用户服务
 *     供其他服务内部调用
 *
 * @author CxCS
 * @Version: 2.0
 */
@FeignClient(name = "cxcsoj-backend-user-service", path = "/api/user/inner")
public interface UserFeignClient {

    /** 【远程接口】
     *  根据 id 获取用户
     *
     * @param userId
     * @return
     */
    @GetMapping("/get/id")
    User getById(@RequestParam("userId") long userId);

    /** 【远程接口】
     *  根据 id 获取用户列表
     *
     * @param ids
     * @return
     */
    @GetMapping("/get/ids")
    List<User> listByIds(@RequestParam("idList") Collection<Long> ids);

    /**
     * 获取当前登录用户
     *
     * @param request
     * @return
     */
    default User getLoginUser(HttpServletRequest request) {
        // 判断是否已登录 (redis session中获取)
        Object userObj = request.getSession().getAttribute(UserConstant.USER_LOGIN_STATE);
        User currentUser = (User) userObj;
        if (currentUser == null || currentUser.getId() == null) {
            throw new BusinessException(ErrorCode.NOT_LOGIN_ERROR);
        }
        return currentUser;
    }

    /**
     * 是否为管理员
     *
     * @param user
     * @return
     */
    default boolean isAdmin(User user) {
        return user != null && UserRoleEnum.ADMIN.getValue().equals(user.getUserRole());
    }

    /**
     * 获取脱敏的用户信息
     *
     * @param user
     * @return
     */
    default UserVO getUserVO(User user) {
        if (user == null) {
            return null;
        }
        UserVO userVO = new UserVO();
        BeanUtils.copyProperties(user, userVO);
        return userVO;
    }

    /** 【远程接口】
     *  通过id获取脱敏用户信息
     *
     * @param userId
     * @return
     */
    @GetMapping("/vo/get/id")
    UserVO getUserVOById(@RequestParam("userId") Long userId);
}
